﻿using ProjScan.Config;
using ProjScan.RecordManager;
using ProjScan.RecordManager.AssetData;
using ProjScan.ScanRule.Attr;
using ProjScan.Tools;
using UnityEngine;

namespace ProjScan.ScanRule.ArtAssets.Mesh
{
    [ArtAssetRuleAttr]
    internal class ImproperPivotPos : RecordRuleImp<MeshRenderingAssetData, MeshRenderingRecordManager>
    {
        private float tolerance = 0.4f;

        protected override bool UseAssetDataCSVHeader => false;

        public ImproperPivotPos()
        {
            ArtAssetsCheckConfig artAssetsCheckConfig = Singleton<ConfigMgr>.Instance.GetConfig<ArtAssetsCheckConfig>() as ArtAssetsCheckConfig;
            tolerance = artAssetsCheckConfig.improperPivotPosTolerance;
        }

        public override void CheckDirSetted()
        {
            if (SeparateDirSettingsEnabled)
            {
                if (target_configs == null || target_configs.Length == 0)
                {
                    ScanLog.UnityLogError("美术资源检测规则 " + GetType().Name + " 执行失败，未设置目标文件夹");
                }

                if (target_configs.Length == 1 && target_configs[0].assetPath.StartsWith("Assets/ArtCheckFolder"))
                {
                    ScanLog.UnityLogError("美术资源检测规则 " + GetType().Name + " 执行失败，未设置目标文件夹");
                }
            }
        }

        public override bool CheckSupportability(out string msg)
        {
            msg = "";
            return true;
        }

        public static float GetBoundBoxMinY(UnityEngine.Mesh mesh)
        {
            return mesh.bounds.min.y;
        }

        protected override bool IsOK(MeshRenderingAssetData assetData)
        {
            return (double)GetBoundBoxMinY(assetData.asset) >= 0.0 - (double)Mathf.Abs(tolerance);
        }
    }
}